package com.ctrl.riveradministrator.mvp.view.activity.problem_list;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BaseListAdapter;
import com.ctrl.riveradministrator.mvp.model.Problem;
import com.ctrl.riveradministrator.mvp.model.Suggest;
import com.ctrl.riveradministrator.mvp.presenter.ProblemListPresenter;
import com.ctrl.riveradministrator.mvp.presenter.ProblemReportPresenter;
import com.ctrl.riveradministrator.mvp.presenter.SuggestPresenter;
import com.ctrl.riveradministrator.mvp.view.activity.problem_report.ProblemReportView;
import com.ctrl.riveradministrator.mvp.view.activity.scenery_share.SceneryDetailActivity;
import com.ctrl.riveradministrator.mvp.view.activity.suggest.SuggestAdapter;
import com.ctrl.riveradministrator.mvp.view.activity.suggest.SuggestUploadActivity;

import java.util.ArrayList;
import java.util.List;

import static com.baidu.mapapi.BMapManager.getContext;

public class ProblemListActivity  extends BaseActivity<ProblemListPresenter> implements ProblemListView,
        BaseListAdapter.OnItemClickListener<Problem> {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;
    private RecyclerView bulletinRl;
    private SwipeRefreshLayout bulletinSfl;
    private LinearLayoutManager layoutManager;
    private ProblemListAdapter problemListAdapter;

    private List<Problem> problems = new ArrayList<>();
    private static int currentPage;

    /**
     * 初始化bundle参数
     *
     * @param parms parms
     */
    @Override
    protected void initParams(Bundle parms) {

    }

    /**
     * 绑定布局
     *
     * @return 布局ID
     */
    @Override
    protected int bindLayout() {
        return R.layout.activity_problem_list;
    }

    /**
     * 初始化控件
     *
     * @param view view
     */
    @Override
    protected void initView(View view) {
        toolbar = $(R.id.toolbar);
        titleTv = $(R.id.title_tv);
        leftMenu = $(R.id.left_iv);
        bulletinRl = $(R.id.bulletin_list);
        bulletinSfl = $(R.id.bulletin_srl);

        layoutManager = new LinearLayoutManager(getContext());
        bulletinRl.setLayoutManager(layoutManager);
        bulletinSfl.setColorSchemeColors(Color.RED, Color.BLUE);
        problemListAdapter = new ProblemListAdapter(this);
        bulletinRl.setAdapter(problemListAdapter);
        bulletinSfl.setEnabled(false);

        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText("问题查看");
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }

    /**
     * 业务操作
     *
     * @param mContext context
     */
    @Override
    protected void doBusiness(Context mContext) {
        currentPage = 0;
        bulletinRl.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (bulletinRl.computeVerticalScrollOffset() > 0&&bulletinRl.computeVerticalScrollExtent() + bulletinRl.computeVerticalScrollOffset()
                            >= bulletinRl.computeVerticalScrollRange()) {
                        currentPage++;
                        mPresenter.getSuggests();
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        mPresenter.getSuggests();
    }

    /**
     * View点击
     *
     * @param view view
     */
    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                finish();
                break;
        }
    }

    /**
     * 设置监听
     */
    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
        problemListAdapter.setOnItemClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected ProblemListPresenter createPresenter() {
        return new ProblemListPresenter(this);
    }


    @Override
    public void onItemClick(Problem problem, int position) {
        startActivity(new Intent(this, ProblemDetail.class).putExtra("problem", problem));
    }

    @Override
    public void showMessage(List<Problem> suggests) {
        this.problems.addAll(suggests);
        problemListAdapter.updateListView(this.problems);
    }
}